package com.iemes.controller.forming;


import java.util.List;
import java.util.Map;

import javax.inject.Inject;


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.iemes.controller.index.BaseController;
import com.iemes.entity.OrderperFormMap;
import com.iemes.entity.ProcessFormMap;
import com.iemes.entity.ResFormMap;

import com.iemes.mapper.ProcessMapper;
import com.iemes.mapper.order.OrderpreMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;
import com.iemes.util.DateUtils;



@Controller
@RequestMapping("instructions")
public class InstructionsController extends BaseController{
	@Inject
	private OrderpreMapper orderpreMapper;
	@Inject
	private ProcessMapper processMapper;
	
	
	@RequestMapping(value = "directive", method = RequestMethod.GET, produces = "text/html; charset=utf-8")
	public String examinepru(Model model) {
		List<ResFormMap> findByRes = findByRes();
		model.addAttribute("res", findByRes);
		return Common.BACKGROUND_PATH + "/forming/instructions/directive_list";	
	}
	@ResponseBody
	@RequestMapping("directive_findByPage")
	public PageView findByPage(String pageNow, String pageSize) {
		OrderperFormMap orderperFormMap =getFormMap(OrderperFormMap.class);
		ProcessFormMap processFormMap = getFormMap(ProcessFormMap.class);
		List<Map<String,Object>> findByPage = orderpreMapper.getInstructions(orderperFormMap);
		for (int i = 0; i < findByPage.size(); i++) {
			//上模时间
			long usedTime = DateUtils.stringToDateTime(findByPage.get(i).get("up_moldTime").toString()).getTime();
			//完成时间
			long installDate = DateUtils.stringToDateTime(findByPage.get(i).get("finish_time").toString()).getTime();
			//当时时间
			long dateTime = DateUtils.getDateTime().getTime();
			//总时间减已用时间得剩于时间
			long timeStamp= (installDate-usedTime)-(dateTime-usedTime);
			long when =timeStamp/(1000*3600);
			//分
			long points =0;
			if (when>0) {
				points= (timeStamp/(1000*60))%60;
			}else if (when==0){
				points= timeStamp/(1000*60)>60?timeStamp/(1000*60)-59:timeStamp/(1000*60);
			}
			//秒
			long seconds =(timeStamp/(1000))-((timeStamp/(1000*60))*60);
			//时：分：秒
			if (points<0 || when<0 || seconds<0) {
				findByPage.get(i).put("countdown_time",0);
			}else {
				findByPage.get(i).put("countdown_time",(when>10?when:"0"+when)+":"+(points>10?points:"0"+points)+":"+(seconds>10?seconds:"0"+seconds));	
			}
			//计算原料耗用数
			processFormMap.put("Machine_ID", findByPage.get(i).get("machine_no"));
			processFormMap.put("Mold_Number", findByPage.get(i).get("now_mold_encode"));
			//processFormMap.put("state", 1);
			List<ProcessFormMap> findByNames = processMapper.findByNames(processFormMap);
			if (findByNames.size()!=0) {
				//拿取用料密度
				Integer density=Integer.parseInt(findByNames.get(findByNames.size()-1).get("Density").toString());
				//拿取套/件 
				String string = findByNames.get(findByNames.size()-1).get("Suite").toString();
				Integer number =Integer.parseInt(string.substring(0, string.lastIndexOf("*")));
				//拿取够产数
				Integer produce=Integer.parseInt(findByPage.get(i).get("order_number").toString());
				//计算原料耗用数
				findByPage.get(i).put("consumption",(produce/number)*density+"g");
			}
			
		}
		try {
			LuogicPaging(pageNow, pageSize, findByPage);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return pageView;
	}
	
	

}
